package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService  {


    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        /**
         * 1:校验参数
         * 2：查询结果 mybatisplus page
         * 3：结果封装到 ResponseResult
         */

        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();


        Page page =new Page(dto.getPage(),dto.getSize()); //分页条件

        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }

        if(dto.getStatus()!=null){
            lambdaQueryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }

        lambdaQueryWrapper.orderByDesc(AdChannel::getCreatedTime);
        lambdaQueryWrapper.orderByDesc(AdChannel::getOrd);
        IPage result = this.page(page, lambdaQueryWrapper);

        ResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(),(int)result.getTotal());
        responseResult.setData(result.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {

        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FAIL);
        }

        adChannel.setCreatedTime(new Date());
        boolean result = this.save(adChannel);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult updateChanel(AdChannel adChannel) {

        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FAIL);
        }

        boolean result = this.updateById(adChannel);

        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        //integer 默认值 null  int 默认值0      int  0/1
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel adChannel = this.getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //实体类 true==1  false==0  1 有效 0 无效
        if(adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //删除
        boolean result = this.removeById(id);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}
